package com.jl.util;

import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class SWTDialogUtil {

	public static void locateToParentCenter(Shell parnt, Shell me) {
		Rectangle parentBounds = parnt.getBounds();
		Rectangle shellBounds = me.getBounds();
		int x = parentBounds.x + (parentBounds.width - shellBounds.width) / 2;
		int y = parentBounds.y + (parentBounds.height - shellBounds.height) / 2;
		if (x > getScreenWidth() - shellBounds.width) {
			x = getScreenWidth() - shellBounds.width;
		}
		if (y > getScreenHeight() - shellBounds.height) {
			y = getScreenHeight() - shellBounds.height;
		}
		if (x < 0) {
			x = 0;
		}
		if (y < 0) {
			y = 0;
		}
		me.setLocation(x, y);
	}

	public static void locateToScreenCenter(Shell me) {
		Display display = Display.getDefault();
		Rectangle displayBounds = display.getPrimaryMonitor().getBounds();
		Rectangle shellBounds = me.getBounds();
		int x = displayBounds.x + (displayBounds.width - shellBounds.width) >> 1;
		int y = displayBounds.y + (displayBounds.height - shellBounds.height) >> 1;
		me.setLocation(x, y);
	}

	public static int getScreenWidth() {
		Display display = Display.getDefault();
		Rectangle displayBounds = display.getPrimaryMonitor().getBounds();
		return displayBounds.width;
	}

	public static int getScreenHeight() {
		Display display = Display.getDefault();
		Rectangle displayBounds = display.getPrimaryMonitor().getBounds();
		return displayBounds.height;
	}
}
